package com.rfqx.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rfqx.entity.City;
import com.rfqx.service.CityService;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;

/**
 * Servlet implementation class CityController
 */
@WebServlet("/cities")
public class CityController extends HttpServlet {

	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CityController() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("application/json");

		PrintWriter out = response.getWriter();

		CityService service = new CityService();

		List<City> cities = service.getCities();
		out.println("{\"cities\" : [ ");
		int i = 0;
		for ( City city : cities ) {
			if (i == 0) {
				out.println( "{ \"name\" : \""  + city.getName() + "\" }" );
				i++;
				continue;
			}	
			
			out.println( ",{ \"name\" : \""  + city.getName() + "\" }" );
		
		}
		out.println("]}");
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
